﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Website/Site1.Master" AutoEventWireup="true" CodeBehind="AllDeals.aspx.cs" Inherits="ChicagoDealsN.Website.AllDeals" EnableEventValidation="false" %>
<%@ Import Namespace="ChicagoDealsN" %>
<%@ Register TagName="Login" Src="~/WebUserControls/Login.ascx" TagPrefix="CD" %>

<script runat="server">
    public void Page_Load(object sender, EventArgs e)
    {
        BizDeals bd = new BizDeals();
        try
        {
            System.Data.DataTable ret = bd.BLGetAllDeals();
            if (ret != null)
            {
                deals.DataSource = ret;
                deals.DataBind();
                deals.Visible = true;
                confirmation.Text = "We've found some deals for you!";
            }
            else { confirmation.Text = "nothing found!";}
        }
        catch (Exception ex)
        {
            confirmation.Text = "Somthing went wrong!<br/>"+ex.ToString();
        }
    }
    
    protected void CommandBtn_Click(Object sender, CommandEventArgs e)
    {
        String id = (String)e.CommandArgument;
        switch(e.CommandName)
        {
            case "Edit":
                id = (String)e.CommandArgument;

                string editId = "EditItem.aspx?id=" + id;
                Response.Redirect(editId);
                break;
            case "Delete":
                BizDeals myBizObject = new BizDeals();
                myBizObject.BLDeleteDeal(Convert.ToInt32(id));
                Response.Redirect(Request.RawUrl);
                break;
            /*
            case "Purchase":
                BizPurchaseHistory bpl = new BizPurchaseHistory();
                DataUser du = Session["UserDet"] as DataUser;
                bool purchased = bpl.purchaseDeal(du.User_Id, Convert.ToInt32(id));
                if (purchased) { confirmation.Text = "Congratulations! You've Purchased a Deal!"; }
                break;
             */ 
            default:
                break;
        }
    }
    
    private bool IsAdmin()
    {
        if (Session["UserDet"] != null)
        {
            DataUser usr = Session["UserDet"] as DataUser;
            return usr.Is_Admin == 0 ? false : true;
        }
        return false;
    }

    private bool IsLoggedIn()
    {
        return (Session["UserDet"] != null)? true : false;
    }

    private bool IsVendor()
    {
        if (Session["UserDet"] != null)
        {
            DataUser du = Session["Userdet"] as DataUser;
             return du.Is_Vendor == 0 ? false : true;
        }
        return false;
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

<div id="conirmation-container">
       <asp:Label id="confirmation" runat="server" />
       <br/>
</div>

<asp:Repeater id="deals" runat="server" Visible="false" OnItemCommand="CommandBtn_Click" >
    <HeaderTemplate>
        <table border="1" width="100%" class="table-style1" cellpadding="4" cellspacing="0">
        <tr bgcolor="#b0c4de">
        <th>DealId</th>
        <th>Name</th>
        <th>Price</th>
        <th>Description</th>
        <th>Img</th>
        <asp:Panel Visible="<%# IsAdmin() %>" runat="server">
            <th>Edit</th>
            <th>Delete</th>
        </asp:Panel>
    
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr bgcolor="#f0f0f0">
            <td><%# DataBinder.Eval(Container.DataItem, "Deal_Id") %></td>
            <td><a href="ViewDeal.aspx?id=<%# DataBinder.Eval(Container.DataItem, "Deal_Id") %>"><%# DataBinder.Eval(Container.DataItem, "Deal_Title") %></a></td>
            <td>$<%# DataBinder.Eval(Container.DataItem, "Deal_Price") %></td>
            <td><%# DataBinder.Eval(Container.DataItem, "Deal_Description") %></td>
            <td><img src="<%# DataBinder.Eval(Container.DataItem, "Img_Path")  %>" alt="dealimg" width="150"/></td>
            <asp:Panel ID="Panel1" Visible="<%# IsAdmin() %>" runat="server">
            <td>
                <asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Deal_Id") %>' OnCommand="CommandBtn_Click" />
            </td>
            <td>
                <asp:Button ID="DeleteButton" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Deal_Id") %>' OnCommand="CommandBtn_Click" />
            </td>
            </asp:Panel>
       
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    <% if(IsLoggedIn() && (IsVendor() || IsAdmin())){ %>
        <div><a href="/website/NewDeal.aspx">Submit a deal</a></div>
    <% } %>
</asp:Content>
